package com.rasenhund.android.grecorder.activity;

import mediba.ad.sdk.android.openx.MasAdListener;
import mediba.ad.sdk.android.openx.MasAdView;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.rasenhund.android.grecorder.R;
import com.rasenhund.android.grecorder.common.Const;
import com.rasenhund.android.myutility.DeployUtil;
import com.rasenhund.android.myutility.MyAdUtil;
import com.rasenhund.android.myutility.MyAdUtil.APPS;

/**
 * Activity 基底クラス
 */
public class BaseActivity extends Activity {
    // @SuppressWarnings("unused")
    // private static final String TAG = BaseActivity.class.getSimpleName();
    private final BaseActivity self = this;

    protected static int mArmyCd = 0; // 軍属コード

    // AdMob
    protected AdView mAdView = null;

    // AdMaker
    protected FrameLayout mAdLayout = null;
    protected MasAdView mMasAdView = null;

    // ヘッダ
    protected TextView mTxtArmyEfsf = null;
    protected TextView mTxtArmyPoz = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // タイトルバーを非表示に
        requestWindowFeature(Window.FEATURE_NO_TITLE);

    }

    @Override
    protected void onResume() {
        super.onResume();

        // 広告の表示開始
        if (mMasAdView != null) {
            mMasAdView.start();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();

        // 広告の表示終了
        if (mMasAdView != null) {
            mMasAdView.stop();
        }

    }

    @Override
    protected void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        if(mMasAdView != null){
            mMasAdView.destroy();
        }
        super.onDestroy();
    }

    /********************
     * Method
     ********************/
    /**
     * String.xmlから文字列を取得
     * 
     * @param id
     * @return
     */
    public String getStr(int id) {
        return getResources().getString(id);
    }

    /**
     * 広告表示部の初期設定
     */
    protected void initAdView() {

        if (DeployUtil.isDebaggable(self)) {
            // デバッグモードの場合は表示しない
            return;
        }

        // 広告表示エリアの取得
        mAdLayout = (FrameLayout) findViewById(R.id.adLayout);
        if (mAdLayout != null) {

            // まずはAdMobを試す
            mAdView = new AdView(self, AdSize.BANNER,
                    MyAdUtil.getAdMob_AdUnitId(MyAdUtil.APPS.GRECORDER));

            // AdViewを追加
            mAdLayout.addView(mAdView);

            // 広告のリクエスト
            AdRequest adRequest = new AdRequest();

            // 開発機はテスト設定にする
            adRequest.addTestDevice("B9F1A5D4CF72541455135F453FED2CD5");

            mAdView.loadAd(adRequest);
            mAdView.setAdListener(new AdListener() {

                @Override
                public void onReceiveAd(Ad arg0) {

                    // AdMobの取得に成功したので可視化する
                    mAdLayout.setVisibility(View.VISIBLE);

                }

                @Override
                public void onPresentScreen(Ad arg0) {

                    // Log.d(TAG, "onPresentScreen");

                }

                @Override
                public void onLeaveApplication(Ad arg0) {

                    // Log.d(TAG, "onLeaveApplication");

                }

                @Override
                public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {

                    // Log.d(TAG, "onFailedToReceiveAd");

                    // AdMobの取得が失敗した場合はAdMakerの取得を試す
                    mMasAdView = (MasAdView) findViewById(R.id.adview);
                    mMasAdView.setAuid(MyAdUtil.getMedibaAd_Auid(APPS.GRECORDER));
                    mMasAdView.setAdListener(new MasAdListener() {

                        @Override
                        public void onFailedToReceiveAd() {
                            // Log.d(TAG, "onFailedToReceiveAd");
                        }

                        @Override
                        public void onFailedToReceiveRefreshedAd() {
                            // Log.d(TAG, "onFailedToReceiveRefreshedAd");
                        }

                        @Override
                        public void onReceiveAd() {
                            // Log.d(TAG, "onReceiveAd");

                            // 広告の取得に成功した場合のみ可視化する
                            mAdLayout.setVisibility(View.VISIBLE);
                            mMasAdView.setVisibility(View.VISIBLE);

                        }

                        @Override
                        public void onReceiveRefreshedAd() {
                            // Log.d(TAG, "onReceiveRefreshedAd");
                        }

                    });

                }

                @Override
                public void onDismissScreen(Ad arg0) {
                    // Log.d(TAG, "onDismissScreen");
                }
            });

        }

    }

    /**
     * 設定(Int型)の書込み
     * 
     * @param key
     * @param value
     */
    protected void setIntPreference(String key, int value) {

        SharedPreferences pref = getPreferences(MODE_PRIVATE);
        Editor editor = pref.edit();

        editor.putInt(key, value);

        editor.commit();

    }

    /**
     * 設定(Int型)の読み込み
     * 
     * @param key
     * @param defValue
     * @return
     */
    protected int getIntPreference(String key, int defValue) {

        SharedPreferences pref = getPreferences(MODE_PRIVATE);
        int value = pref.getInt(key, defValue);

        return value;

    }

    /**
     * 軍属に応じてヘッダを変更
     * 
     * @param armyCd
     */
    protected void setArmyHeaderVisibility(int armyCd) {

        if (mTxtArmyEfsf == null || mTxtArmyPoz == null)
            return;

        if (mArmyCd == Const.ARMY.EFSF) {

            mTxtArmyEfsf.setVisibility(View.VISIBLE);
            mTxtArmyPoz.setVisibility(View.GONE);

        } else if (mArmyCd == Const.ARMY.POZ) {

            mTxtArmyEfsf.setVisibility(View.GONE);
            mTxtArmyPoz.setVisibility(View.VISIBLE);

        }

    }

}
